package com.resource.manager.ui;

import cn.hutool.core.util.ObjectUtil;
import com.resource.manager.util.SwingUtil;
import lombok.SneakyThrows;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ImagePanel extends JPanel {

    /**
     * 并发编程的list
     */
    public static final List<ImageCache> cache = Collections.synchronizedList(new LinkedList<>());

    private BufferedImage image;

    private static ThreadPoolExecutor executors = new ThreadPoolExecutor(2, 8, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(100));

    @SneakyThrows
    public ImagePanel(String path) {
        this.setLayout(null);
        this.setBackground(Color.WHITE);
        executors.execute(() -> {
            image = getImage(path);
            this.repaint();
        });
    }

    private BufferedImage getImage(String path) {
        cacheRefresh();
        ImageCache imageCache = cache.stream().filter(c -> c.getPath().equals(path)).findFirst().orElse(null);
        if (imageCache != null) {
            return imageCache.getImage();
        }
        try {
            BufferedImage read = ImageIO.read(new File(path));
            BufferedImage yasuo = SwingUtil.compressInMemory(read);
            //压缩图片大小
            cache.add(new ImageCache(path, yasuo));
            return yasuo;
        } catch (Exception e) {
            try {
                BufferedImage read = ImageIO.read(Objects.requireNonNull(ImagePanel.class.getClassLoader().getResourceAsStream(path)));
                cache.add(new ImageCache(path, read));
                return read;
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        } finally {
            //提醒gc垃圾回收
            System.gc();
        }
    }

    private void cacheRefresh() {
        //缓存最多放100条 超过了100条,则从头部删除多余的
        if (cache.size() > 1000) {
            cache.remove(0);
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (ObjectUtil.isEmpty(image)) {
            return;
        }
        int width = image.getWidth();
        int height = image.getHeight();
        //绘制图片,同比例缩放至面板最大可展示面积,居中展示
        if (width > getWidth() || height > getHeight()) {
            double scale = Math.min(getWidth() / (double) width, getHeight() / (double) height);
            //根据宽度高度居中绘制
            g.drawImage(image, (getWidth() - (int) (width * scale)) / 2, (getHeight() - (int) (height * scale)) / 2, (int) (width * scale), (int) (height * scale), null);
//            g.drawImage(image, getX(), getY(), (int) (width * scale) - 10, (int) (height * scale) - 10, null);
            return;
        }
        g.drawImage(image, getX(), getY(), getWidth(), getHeight(), null);
    }

}
